﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using uk.ac.soton.ses.DAL;
using System.Collections.Generic;
using System.Web;

namespace uk.ac.soton.ses.ExperimentActionsWebPart
{
    public partial class ExperimentActionsWebPartUserControl : UserControl
    {
        private readonly static string ReportUrl = "/_layouts/HDC/ReportPrintout.aspx";

        private HDCPlugins<IHDCExperimentPlugin> _plugins = null;
        private int _experimentID;
        private MaterialsMetadataContext _dbcontext;

        public int ExperimentID { get { return this._experimentID; } set { this._experimentID = value; } }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this._plugins = new HDCPlugins<IHDCExperimentPlugin>();
            this._dbcontext = HttpContext.Current.GetMaterialsDBContext();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.DataBindPlugins();
            }
        }

        private void DataBindPlugins()
        {
            //Only bind if we don't have any plugins
            if (this.List_ExperimentPlugins.Items.Count == 0)
            {
                this.List_ExperimentPlugins.Items.Add(new ListItem("Select a plugin to execute:"));
                foreach (IHDCExperimentPlugin plugin in this._plugins.Plugins)
                {
                    this.List_ExperimentPlugins.Items.Add(new ListItem(plugin.Name, plugin.GetType().AssemblyQualifiedName));
                }
            }
        }

        /// <summary>
        /// Event handler for when the user selects a plugin from the drop down menu
        /// </summary>
        /// <param name="sender">The control that generated the event</param>
        /// <param name="e">The event arguments</param>
        protected void ExecuteExperimentPlugin(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            if (ddl.SelectedIndex > 0)
            {
                try
                {
                    IHDCExperimentPlugin selectedImportPlugin = this._plugins.Plugins[ddl.SelectedIndex - 1];
                    if (selectedImportPlugin.GetType().AssemblyQualifiedName == ddl.SelectedValue)
                    {
                        selectedImportPlugin.ExecutePlugin(this._experimentID, this, this._dbcontext);
                    }
                }
                catch (Exception ex)
                {
                    string errorMessage = String.Format("Plug in failed with error: {0}", ex.Message);
                    HDCSharedWebFunctions.AddErrorLabel(errorMessage, this.PlaceHolder_Messages);
                }
            }
        }
    }
}
